<script setup>
// import { GetListComeAPI } from '@/api/statistics/procure/orderTack.js'
import { GetCustomSelsctAPI } from '$/common/index'

// 列表数据
const tableData = ref([])

// 按钮
const buttonList = ref([{ type: 'export', hasPermi: 'finance:dataChange:export' }])

// 列表枚举
const column = ref([
  { label: '客户编码', prop: 'clientCode', width: '180' },
  { label: '客户名称', prop: 'clientName', width: '180' },
  { label: '客户类别', prop: 'typeName' },
  { label: '期初余额', prop: 'balanceAmount' },
  { label: '销售余额', prop: 'amount' },
  { label: '应收金额', prop: 'closedAmount' },
  { label: '整单折扣', prop: 'packageDiscount' },
  { label: '实际收款余额', prop: 'collectionAmount' },
  { label: '应收款余额', prop: 'balanceOfAccounts' }
])

// 状态
const state = reactive({
  treeLoading: false,
  loading: false, // 列表加载状态
  btnLoading: false, // 按钮加载状态
  isShow: false, // 弹窗状态
  title: '新增', // 弹窗标题
  total: 0 // 列表总量
})

// 查询条件
const query = reactive({
  pageNum: 1,
  pageSize: 10
})

// 查询条件 枚举
const selectList = ref([
  { label: '开单时间', type: '日期范围' },

  {
    label: '客户',
    model: 'materialCode',
    type: '输入框'
  },
  {
    label: '客户类别',
    model: 'materialName',
    type: '下拉单选'
  }
])

// 导出
const exportFun = () => {}

// 查询条件
const selectSubmit = val => {
  Object.keys(val).forEach(keyItem => (query[keyItem] = val[keyItem]))
  getList()
}

// 获取客户下拉数据
const getCustomSelsct = async () => {
  const { data: res } = await GetCustomSelsctAPI()
  selectList.value[2].opt = res
}
getCustomSelsct()

// 获取列表
const getList = async () => {
  query.beginTime = '2023-07-01'
  query.endTime = '2023-9-30'
  query.type = '1'
  state.loading = true
  try {
    const result = await GetListComeAPI(query)
    tableData.value = result.rows
    state.total = result.total
  } catch {
  } finally {
    state.loading = false
  }
}
getList()

const summaryMethod = param => {
  const { columns, data } = param
  const sums = []
  columns.forEach((column, index) => {
    if (index === 0) {
      sums[index] = '合计'
      return
    }

    const values = data.map(item => {
      // 掺入数组对比
      if (
        ['amount', 'closedAmount', 'packageDiscount', 'collectionAmount', 'balanceOfAccounts'].includes(column.property)
      ) {
        return Number(item[column.property])
      } else {
        return '-?-'
      }
    })
    // 应付款
    if (index === 9) {
      console.log(values)
      sums[index] = values[values.length] || '--'
      return
    }
    if (!values.every(value => value == '-?-')) {
      sums[index] = values.reduce((prev, curr) => {
        const value = Number(curr)
        if (!Number.isNaN(value)) {
          const number = Number(prev) + Number(curr)
          return number.toFixed(2)
        } else {
          return prev
        }
      }, 0)
    } else {
      sums[index] = '/'
    }
  })

  return sums
}
</script>

<template>
  <div class="app-container">
    <!-- 查询条件组件 -->
    <p-select-form :selectForm="selectList" @selectSubmit="selectSubmit" />
    <el-card>
      <!-- 按钮组件 -->
      <p-button :list="buttonList" @add="addDic" />
      <!-- 表格数据 -->
      <p-table
        class="tableClass"
        summary
        summaryMethodYes
        :summaryMethod="summaryMethod"
        :isSel="false"
        :loading="state.loading"
        :data="tableData"
        :column="column"
        v-model:page="query.pageNum"
        v-model:limit="query.pageSize"
        :total="state.total"
        @change="handleSelectionChange"
        @pagination="getList"
      >
        <template #columnCell="{ column, record }">
          <!-- 辅助属性 -->
          <p-producer :data="record.auxiliaryAttribute" v-if="column.label == '辅助属性'" />
        </template>
      </p-table>
    </el-card>
  </div>
</template>
<style scoped lang="scss"></style>
